package com.atom.common.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.atom.common.entity.CommentDO;
import com.atom.common.remote.user.UserUtil;
@Repository
public class CommentDao extends AvensCommonDao<CommentDO> {

  public CommentDO findByTaskId(String taskId) {
    List<CommentDO> list = super.findByProperty("taskId", taskId);
    if (list == null || list.size() == 0) {
      return null;
    } else {
      return list.get(0);
    }
  }
  
  public void saveComment(String yourAdvice, UserUtil user, String creator, int type, String taskId, String processInstanceId, String taskDefinitionKey) {
    CommentDO instance = new CommentDO();
    instance.setCommentCreator(creator);
    instance.setCommentText(yourAdvice);
    instance.setCommentTime(new Date());
    instance.setCommentType(type);
    instance.setCommentUser(user.getUserid());
    instance.setCommentUserText(user.getUsername());
    instance.setCommentUserOrg(user.getOrgid());
    instance.setProcessInstanceId(processInstanceId);
    instance.setTaskId(taskId);
    instance.setTaskDefinitionKey(taskDefinitionKey);
    super.save(instance);
  }

  public List<CommentDO> findByProcessInstanceId(String processInstanceId) {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("processInstanceId", processInstanceId);
    return super.findByProperties(properties);
  }
  
  public List<CommentDO> findByTypeByProcessInstanceId(int commentType, String processInstanceId) {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("processInstanceId", processInstanceId);
    properties.put("commentType", commentType);
    return super.findByProperties(properties);
  }
  
  public List<CommentDO> findByTypeByProcessInstanceIdByCreator(int commentType, String processInstanceId, String commentCreator) {
    Map<String, Object> properties = new HashMap<String, Object>();
    properties.put("processInstanceId", processInstanceId);
    properties.put("commentType", commentType);
    properties.put("commentCreator", commentCreator);
    return super.findByProperties(properties);
  }


}
